package com.listeningframework.boot.commons.encrypt;

import com.listeningframework.boot.constants.Encoding;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 封装各种格式的编码解码工具类.
 * 
 * 1.Commons-Codec的 hex/base64 编码 2.自制的base62 编码 3.Commons-Lang的xml/html escape
 * 4.JDK提供的URLEncoder
 * 
 */
public class EncodeUtils {
    /**
     * Hex编码.
     * @param input 字节数组
     * @return String
     */
    public static String encodeHex(byte[] input) {
        return Hex.encodeHexString(input);
    }

    /**
     * Hex解码.
     *
     * @param input 待解密字符串
     * @return byte[]
     */
    public static byte[] decodeHex(String input) {
        try {
            return Hex.decodeHex(input.toCharArray());
        } catch (DecoderException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Base64编码.
     *
     * @param input 字节数组
     * @return String
     */
    public static String encodeBase64(byte[] input) {
        return Base64.encodeBase64String(input);
    }

    /**
     * Base64编码.
     *
     * @param input 待加密字符串
     * @return String
     */
    public static String encodeBase64(String input) {
        return Base64.encodeBase64String(getBytes(input));
    }

    /**
     * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).
     *
     * @param input 字节数组
     * @return String
     */
    public static String encodeUrlSafeBase64(byte[] input) {
        return Base64.encodeBase64URLSafeString(input);
    }

    /**
     * Base64解码.
     *
     * @param input 字符串
     * @return byte[]
     */
    public static byte[] decodeBase64(String input) {
        return Base64.decodeBase64(input);
    }

    /**
     * Base64解码.
     * @param input 待处理字符串
     * @return String
     */
    public static String decodeBase64ToString(String input) {
        return getString(Base64.decodeBase64(input));
    }

    /**
     * URL 编码, Encode默认为UTF-8.
     *
     * @param part 待处理字符串
     * @return String
     */
    public static String urlEncode(String part) {
        try {
            return URLEncoder.encode(part, Encoding.UTF8.getCode());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * URL 解码, Encode默认为UTF-8.
     *
     * @param part 待处理字符串
     * @return String
     */
    public static String urlDecode(String part) {
        try {
            return URLDecoder.decode(part, Encoding.UTF8.getCode());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取字符串的字节数组
     *
     * @param content 字符串
     * @return byte[]
     */
    public static byte[] getBytes(String content) {
        try {
            return content.getBytes(Encoding.UTF8.getCode());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 转换字节数组为字符串
     *
     * @param content 字节数组
     * @return String
     */
    public static String getString(byte[] content) {
        try {
            return new String(content, Encoding.UTF8.getCode());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 指定编码方式转换字节数组为字符串
     *
     * @param content 字符串字节数组
     * @param encoding 编码
     * @return String
     */
    public static String getString(byte[] content, Encoding encoding) {
        try {
            return new String(content, encoding.getCode());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

}
